﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NCS.IConnect.ApplicationContexts;
using Microsoft.SharePoint;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using JM.JCY.Common;
using System.Data;
using JM.JCY.Common.Utilities;
using System.IO;
using System.Threading;
using System.Text;

namespace JM.JCY.SharePointComponent.PhotoAlbum.EditAlbum
{
    public partial class EditAlbumUserControl : UserControl
    {
        public int _staffId
        {
            set;
            get;
        }
        public string _albumId
        {
            get
            {
                if (ViewState["Edit_albumId"] != null)
                {
                    return (string)ViewState["Edit_albumId"];
                }
                else
                {
                    return "";
                }
            }
            set { ViewState["Edit_albumId"] = value; }
        }

        public string _SpFolderId
        {
            get
            {
                if (ViewState["Edit_SpFolderId"] != null)
                {
                    return (string)ViewState["Edit_SpFolderId"];
                }
                else
                {
                    return "";
                }
            }
            set { ViewState["Edit_SpFolderId"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadApplicationContext();
            if (!IsPostBack)
            {
                if (Request.Params["albumId"] != null)
                {
                    _albumId = Request.Params["albumId"];

                    using (PhotoAlbumProxy accessProxy = new PhotoAlbumProxy())
                    {
                        var ds = accessProxy.getAlbumDbyAlbumId(Convert.ToInt32(_albumId));
                        txtAlbumName.Text = ds.T_Album[0].Name;
                        txtAlbumDescription.Text = ds.T_Album[0].Description;
                        _SpFolderId = ds.T_SharePointObject[0].SPFolderId;
                    }
                }
            }
        }

        private void LoadApplicationContext()
        {
            if (ApplicationContext.Current.GetKeyValue("userId") != null)
            {
                _staffId = Convert.ToInt32(ApplicationContext.Current.GetKeyValue("userId"));
            }
        }

        protected void ImgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string gotoUrl = string.Empty;
                using (PhotoAlbumProxy funProxy = new PhotoAlbumProxy())
                {
                    //update album table
                    var albumDs = funProxy.getAlbumDbyAlbumId(Convert.ToInt32(_albumId));
                        string oldUrlAlbumName = albumDs.T_Album[0].Name;
                        string newAlbumName = txtAlbumName.Text.Trim();

                        string oldDescription = albumDs.T_Album[0].Description;
                        string newDescription = txtAlbumDescription.Text.Trim();

                        albumDs.T_Album[0].Name = newAlbumName;
                        albumDs.T_Album[0].Description = this.txtAlbumDescription.Text.Length > 256 ? this.txtAlbumDescription.Text.Substring(0, 256) : this.txtAlbumDescription.Text;
                        albumDs.T_Album[0].LatestUpdateTime = DateTime.Now;
                        albumDs.T_Album[0].LAST_UPDATED_TIME = DateTime.Now;
                        albumDs.T_Album[0].LAST_UPDATED_BY = _staffId.ToString();

                        funProxy.CreateAlbumwithUpload(albumDs);

                        gotoUrl = string.Format("ViewAlbum.aspx?AlbumId={0}", albumDs.T_Album[0].AlbumId);
    
                }
                Response.Redirect(gotoUrl);
            }
            catch (Exception ex)
            {
                bool isok = JCYUtility.ExceptionHandle("edite album", ex);
                if (isok == false)
                {
                    throw ex;
                }
            }
        }
    }
}
